package mwa.cs.dao;

import java.util.List;
import java.util.Map;

import mwa.cs.database.Database;
import mwa.cs.model.MeterBrand;

/**
 * A class used for managing MeterBrand class
 */
public class MeterBrandDAO {

    /** a database of this meter_brand */
    private Database db;

    /**
     * Instantiate a new object of this class
     * @param database a database
     */
    public MeterBrandDAO(Database db) {
        this.db = db;
    }

    /**
     * a method for searching all meter_brand in database
     * @return all meter_brand
     */
    public List<Map<String, Object>> findAll() {
        String vaSql = "SELECT * FROM meter_brand";
        List<Map<String, Object>> result = db.queryList(vaSql);
        if (result != null) {
            return result;
        } else {
            return null;
        }
    }
    
    /**
     * a method for searching meter_brand by siteId in database
     * @return all meter_brand in siteId
     */
    public List<Map<String, Object>> findMeterBrandBySiteId(int pnSiteId) {
        String vaSql = "SELECT meter_brand_code, meter_brand_description FROM meter_brand WHERE site_id = ? ORDER BY meter_brand_code ASC";
        List<Map<String, Object>> result = db.queryList(vaSql, pnSiteId);
        if (result != null) {
            return result;
        } else {
            return null;
        }
    }
    
    public String findDescriptionBySiteAndCode(int pnSiteId, String paMeterBrandCode) {
        String vaSql = "SELECT meter_brand_description FROM meter_brand WHERE site_id = ? and meter_brand_code = ?";
        Map<String, Object> result = db.querySingle(vaSql, pnSiteId, paMeterBrandCode);
        if (result != null) {
            return (String) result.get("METER_BRAND_DESCRIPTION");
        } else {
            return null;
        }
    }
	

    //get meter_brand_code for check duplicate for insert data
    public String findZoneIdBySiteAndZoneId(int pnSiteId, String paMeterBrandCode) {
        String vaSql = "SELECT meter_brand_code  FROM  meter_brand WHERE site_id = ? and  meter_brand_code = ? ";
        Map<String, Object> result = db.querySingle(vaSql, pnSiteId, paMeterBrandCode);
        if (result != null) {
            return (String) result.get("METER_BRAND_CODE").toString();
        } else {
            return null;
        }
    }
    
    //get meter_brand_code for check duplicate for update data
    public String findsMeterBrandCodeBySiteAndMeterBrandCode(int pnSiteId, String paOldMeterBrandCode,String paNewMeterBrandCode) {
        String vaSql = "SELECT meter_brand_code FROM meter_brand WHERE site_id = ? and meter_brand_code != ? and meter_brand_code = ?";
        Map<String, Object> result = db.querySingle(vaSql, pnSiteId,paOldMeterBrandCode,paNewMeterBrandCode);
        if (result != null) {
            return (String) result.get("METER_BRAND_CODE");
        } else {
            return null;
        }
    }
    
    
    //insert meter_brand
    public void addMeterBrand(MeterBrand poMeterBrand) {
   	 String vaSql = "INSERT INTO METER_BRAND (SITE_ID,METER_BRAND_CODE,meter_brand_description,INSERT_USER_ID,INSERT_DATE,INSERT_TIME,"
   	 								+ "UPDATE_USER_ID,UPDATE_DATE,UPDATE_TIME)"
   			 						+"VALUES(?,?,?,?,?,?,?,?,?)";
   			try {
   					db.insert(vaSql, poMeterBrand.getVnSiteId(),
   							poMeterBrand.getVaMeterBrandCode(),
   							poMeterBrand.getVaMeterBrandDescription(),
   							poMeterBrand.getVnInsertUserId(),
   							poMeterBrand.getVaInsertDate(),
   							poMeterBrand.getVaInsertTime(),
   							poMeterBrand.getVnUpdateUserId(),
   							poMeterBrand.getVaUpdateDate(),
   							poMeterBrand.getVaUpdateTime());
   				
   			}catch (Exception e) {
   	        	throw new RuntimeException(e);
   	        }
   	}
    
    //update MeterBrand  
    public void updateMeterBrand(int pnSiteId, String paMeterBrandCode,MeterBrand poMeterBrand) {
	 	 
    	String vaSql  = " UPDATE METER_BRAND SET " +
    				  " METER_BRAND_CODE = ? , " +
    				  " METER_BRAND_DESCRIPTION  = ? , " +
    				  " UPDATE_USER_ID = ? , "+
    				  " UPDATE_DATE = ? , " +
    				  " UPDATE_TIME = ? " +
    				  " WHERE " + 
    				  " METER_BRAND_CODE  = ? " +
    				  " AND SITE_ID = ? ";
    	
    	 try {
				db.update(vaSql, poMeterBrand.getVaMeterBrandCode(), 
						poMeterBrand.getVaMeterBrandDescription(),
						poMeterBrand.getVnUpdateUserId(),
						poMeterBrand.getVaUpdateDate(),
						poMeterBrand.getVaUpdateTime(),
						paMeterBrandCode,
						pnSiteId);
							
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
    }
    
    //delete meter_brand
    public boolean deleteMeterBrandBySiteId(int pnSiteId,String paMeterBrandCode) {
        String vaSql = "DELETE FROM METER_BRAND WHERE SITE_ID = ? and METER_BRAND_CODE = ?";
        try {
            db.update(vaSql, pnSiteId,paMeterBrandCode);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    
}
